package com.caishi.lkx.exam.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.context.RequireContext;
import com.caishi.lkx.common.context.Role;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.exam.service.IExamService;
import com.caishi.lkx.exam.service.IIndustryService;
import com.caishi.lkx.exam.service.impl.AdvertisementServiceImpl;
import com.caishi.lkx.user.model.AdvertisementModel;
import com.caishi.lkx.user.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author by liyihua
 * @date : 2022-01-06 14:29
 */
@Tag(name = "广告相关接口")
@RestController
@RequestMapping("/advertisement")
public class AdvertisementApi extends AdminBaseApi<AdvertisementServiceImpl, AdvertisementModel, String> {
    @Resource
    private AdvertisementServiceImpl advertisementService;
    @Resource
    private IExamService examService;
    @Resource
    private IIndustryService industryService;
    @Resource
    private IUserService userService;

    @Override
    public AdvertisementServiceImpl loadService() {
        return advertisementService;
    }

    @Override
    @PostMapping("/update")
    @RequireContext({com.caishi.lkx.common.context.Role.APP, Role.CHANNEL})
    public AdvertisementModel add(@RequestBody AdvertisementModel entity)  {
        entity.setGroupId(context.getGroupId());
        return loadService().insert(entity);
    }
    @Override
    @GetMapping("/page")
    @RequireContext({com.caishi.lkx.common.context.Role.APP, Role.CHANNEL})
    public IPage<AdvertisementModel> page(@ModelAttribute Page<AdvertisementModel> page, @ModelAttribute AdvertisementModel model, @NotNull JSONObject otherParam) {
        model.setGroupId(context.getGroupId());
        IPage<AdvertisementModel> data = loadService().page(loadService().pageProcessing(page), baseWrapper(model, otherParam));
        data.setRecords(listCover(data.getRecords(), otherParam));
        return data;
    }

    @Override
    protected LambdaQueryWrapper<AdvertisementModel> baseWrapper(AdvertisementModel model, JSONObject otherParam) {
        return super.baseWrapper(
                        model.clearEmptyStringField("title"), otherParam)
                .like(StrUtil.isNotBlank(model.getTitle()), AdvertisementModel::getTitle, model.getTitle());
    }

    @Override
    protected List<AdvertisementModel> listCover(List<AdvertisementModel> result, JSONObject otherParam) {
        return   result.parallelStream().peek(r->{
            r.setExamName(examService.getById(r.getExamId())!=null?examService.getById(r.getExamId()).getName():null);
            r.setIndustryName(industryService.getById(r.getIndustryId())!=null?industryService.getById(r.getIndustryId()).getName():null);
            r.setCreatedName(userService.getUserNameById(r.getCreatedBy()));
        }).collect(Collectors.toList());

    }
    @Operation(summary = "获取广告列表")
    @GetMapping("/list")
    @Roles
    @RequireContext(com.caishi.lkx.common.context.Role.APP)
    public List<AdvertisementModel> list(@RequestParam(defaultValue = "10") Integer size, @ModelAttribute AdvertisementModel model, @NotNull JSONObject otherParam) {
        if (size > 1000) {
            size = 1000;
        }
        model.setGroupId(context.getGroupId());
        return listCover(loadService().selectList(loadService().wrapperProcessing(baseWrapper(model, otherParam)), size), otherParam);
    }

    @DeleteMapping("/batch-delete")
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary = "批量删除")
    public Boolean batchDelete(@RequestParam List<String> ids) {
        return advertisementService.delete(ids);
    }

}
